//
//  MBModelDetailHeaderView.m
//  Meboo
//
//  Created by kyo on 2024/3/11.
//

#import "MBModelDetailHeaderView.h"
#import <RongCloudOpenSource/RongIMKit.h>
#import "MBChatListViewController.h"
@interface MBModelDetailHeaderView ()
@property(nonatomic,strong) UIButton *backBtn;
@property(nonatomic,strong) UIButton *messageBtn;

@property(nonatomic,strong) UIImageView *vipImageView;
@property(nonatomic,strong) UIImageView *iconImageView;
//虚化背景
@property(nonatomic,strong) UIImageView *topBackImageView;


@property(nonatomic,strong) UILabel *nameLab;
@property(nonatomic,strong) UILabel *interestsLab;

@property(nonatomic,strong) UIButton *likeBtn;
@property(nonatomic,strong) UIButton *passBtn;

@end

@implementation MBModelDetailHeaderView

#pragma mark - Life Cycle
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}


#pragma mark - Public Metohd
- (void)setModel:(MBWLMTopicModel *)model {
    _model = model;
    MBWLMTopicImageModel *imageModel = model.images[0];
    [self.topBackImageView sd_setImageWithURL:[NSURL URLWithString:imageModel.imageUrl]];
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:imageModel.imageUrl] placeholderImage:ImageWithName(@"app_defult_image")];
    self.nameLab.text = [NSString stringWithFormat:@"%@,%@",model.nickName,model.age];
    
    NSString *interestsStr = @"";
    for (MBWLMTopicImageModel *intModel in model.interests) {
        if (interestsStr.length > 0) {
            interestsStr = [NSString stringWithFormat:@"%@, %@",interestsStr,intModel.interest];
        } else {
            interestsStr = [NSString stringWithFormat:@"%@%@",interestsStr,intModel.interest];
        }
    }
    self.interestsLab.text = interestsStr;
    
    if (model.likePassState == 1) {
        self.likeBtn.layer.borderWidth = 0;
        [self.likeBtn setBackgroundColor:HEXS(@"B92176")];
        [self disableBtn];
    } else if (model.likePassState == 2) {
        self.passBtn.layer.borderWidth = 0;
        [self.passBtn setBackgroundColor:HEXS(@"B92176")];
        [self disableBtn];
    }
}

#pragma mark - Action
- (void)messageAction {
    NSArray *displayConversationTypeArray = @[@(ConversationType_PRIVATE)];
    //        RCConversationListViewController *conversationListVC = [[RCConversationListViewController alloc] initWithDisplayConversationTypes:displayConversationTypeArray
    //                                                                                               collectionConversationType:nil];
    MBChatListViewController *conversationListVC = [[MBChatListViewController alloc] initWithDisplayConversationTypes:displayConversationTypeArray
                                                                                           collectionConversationType:nil];
    [conversationListVC setConversationAvatarStyle:RC_USER_AVATAR_CYCLE];
    [self.currentViewController.navigationController pushViewController:conversationListVC animated:YES];
}

- (void)backAction {
    [self.currentViewController.navigationController popViewControllerAnimated:YES];
}

- (void)likeAction {
    self.likeBtn.layer.borderWidth = 0;
    [self.likeBtn setBackgroundColor:HEXS(@"B92176")];
    [[NSNotificationCenter defaultCenter] postNotificationName:MODEL_DETAIL_CLICK_LIKEPASS object:@1];
    [self disableBtn];
}

- (void)passAction {
    self.passBtn.layer.borderWidth = 0;
    [self.passBtn setBackgroundColor:HEXS(@"B92176")];
    [[NSNotificationCenter defaultCenter] postNotificationName:MODEL_DETAIL_CLICK_LIKEPASS object:@2];
    [self disableBtn];
}

- (void)disableBtn {
    self.likeBtn.enabled = NO;
    self.passBtn.enabled = NO;
}

#pragma mark - UI
- (void)setupUI {
    self.backgroundColor = UIColor.clearColor;
    
    
    [self addSubview:self.topBackImageView];
    [self.topBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    //2，iOS 系统自带的高斯模糊效果
    UIBlurEffect *blurEffrct =[UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemThinMaterialDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:blurEffrct];
    effectView.backgroundColor = [UIColor colorWithRed:140 / 255.0 green:72 / 255.0 blue:209 / 255.0 alpha:0.3];
    effectView.alpha = 0.84;
    effectView.frame = self.bounds;
    [self addSubview:effectView];
    
    
    [self addSubview:self.backBtn];
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBWidthScale(20));
        make.top.mas_equalTo(StatusBarHeight+MBHeightScale(12));
        make.width.height.mas_equalTo(MBHeightScale(20));
    }];
    
    [self addSubview:self.messageBtn];
    [self.messageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backBtn);
        make.right.mas_equalTo(MBWidthScale(-20));
//        make.width.mas_equalTo(MBWidthScale(37));
//        make.height.mas_equalTo(MBHeightScale(26));
    }];
    
    [self addSubview:self.iconImageView];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backBtn.mas_bottom).offset(MBHeightScale(24));
        make.left.mas_equalTo(MBWidthScale(22));
        make.width.height.mas_equalTo(MBHeightScale(50));
    }];
    
    [self addSubview:self.vipImageView];
    [self.vipImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(MBHeightScale(19));
        make.top.equalTo(self.backBtn.mas_bottom).offset(MBHeightScale(19));
        make.width.height.mas_equalTo(MBHeightScale(19));
    }];
    
    [self addSubview:self.nameLab];
    [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView);
        make.left.equalTo(self.iconImageView.mas_right).offset(MBWidthScale(15));
        make.right.mas_equalTo(MBWidthScale(-9));
    }];
    
    [self addSubview:self.interestsLab];
    [self.interestsLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLab.mas_bottom).offset(MBHeightScale(5));
        make.left.equalTo(self.iconImageView.mas_right).offset(MBWidthScale(15));
        make.right.mas_equalTo(MBWidthScale(-9));
    }];
    
    [self addSubview:self.likeBtn];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(MBHeightScale(12));
        make.left.mas_equalTo(MBWidthScale(25));
        make.right.equalTo(self.mas_centerX).offset(MBWidthScale(-6));
        make.height.mas_equalTo(MBHeightScale(30));
    }];
    
    [self addSubview:self.passBtn];
    [self.passBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(MBHeightScale(12));
        make.right.mas_equalTo(MBWidthScale(-25));
        make.left.equalTo(self.mas_centerX).offset(MBWidthScale(6));
        make.height.mas_equalTo(MBHeightScale(30));
    }];
    
}

#pragma mark - Setter && Getter
- (UIButton *)messageBtn {
    if (!_messageBtn) {
        _messageBtn = [[UIButton alloc] init];
        [_messageBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_messageBtn setBackgroundImage:ImageWithName(@"common_chat") forState:0];
        [_messageBtn addTarget:self action:@selector(messageAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _messageBtn;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        _backBtn = [UIButton new];
        [_backBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        [_backBtn setBackgroundImage:ImageWithName(@"mine_topic_back") forState:0];
        [_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backBtn;
}

- (UILabel *)nameLab {
    if (!_nameLab) {
        _nameLab = [UILabel new];
        _nameLab.font = PDSemiBoldScale(16);
        _nameLab.textColor = UIColor.whiteColor;
    }
    return _nameLab;
}

- (UILabel *)interestsLab {
    if (!_interestsLab) {
        _interestsLab = [UILabel new];
        _interestsLab.font = PDRegularScale(12);
        _interestsLab.textColor = UIColor.whiteColor;
    }
    return _interestsLab;
}

- (UIImageView *)vipImageView {
    if (!_vipImageView) {
        _vipImageView = [UIImageView new];
        _vipImageView.image = ImageWithName(@"modellist_vip_icon");
//        _vipImageView.hidden = YES;
    }
    return _vipImageView;
}

- (UIImageView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [UIImageView new];
        _iconImageView.image = ImageWithName(@"flower_defult");
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        _iconImageView.layer.cornerRadius = MBHeightScale(50/2);
        _iconImageView.layer.borderColor = UIColor.whiteColor.CGColor;
        _iconImageView.layer.borderWidth = 2;
        _iconImageView.layer.masksToBounds = YES;
    }
    return _iconImageView;
}

- (UIImageView *)topBackImageView {
    if (!_topBackImageView) {
        _topBackImageView = [UIImageView new];
        _topBackImageView.contentMode = UIViewContentModeScaleAspectFill;
        [_topBackImageView setClipsToBounds:YES];
    }
    return _topBackImageView;
}

- (UIButton *)likeBtn {
    if (!_likeBtn) {
        _likeBtn = [UIButton new];
        [_likeBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        _likeBtn.layer.cornerRadius = MBHeightScale(15);
        [_likeBtn setTitle:@"follow" forState:0];
        _likeBtn.titleLabel.font = PDSemiBoldScale(14);
        [_likeBtn setTitleColor:UIColor.whiteColor forState:0];
        [_likeBtn setBackgroundColor:UIColor.clearColor];
        _likeBtn.layer.borderWidth = 1;
        _likeBtn.layer.borderColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:0.5].CGColor;
        [_likeBtn addTarget:self action:@selector(likeAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _likeBtn;
}

- (UIButton *)passBtn {
    if (!_passBtn) {
        _passBtn = [UIButton new];
        [_passBtn setYXEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
        _passBtn.layer.cornerRadius = MBHeightScale(15);
        [_passBtn setTitle:@"Not interested" forState:0];
        _passBtn.titleLabel.font = PDSemiBoldScale(14);
        [_passBtn setTitleColor:UIColor.whiteColor forState:0];
        [_passBtn setBackgroundColor:UIColor.clearColor];
        _passBtn.layer.borderWidth = 1;
        _passBtn.layer.borderColor = [UIColor colorWithRed:255 / 255.0 green:255 / 255.0 blue:255 / 255.0 alpha:0.5].CGColor;
        [_passBtn addTarget:self action:@selector(passAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _passBtn;
}

@end

